package com.sxw.hw_student.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxw.hw_student.system.entity.StudentCourse;
import com.sxw.hw_student.system.entity.SysRole;
import com.sxw.hw_student.system.service.IStudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生课程关联 控制器
 */
@RestController
@RequestMapping("/studentCourse")
public class StudentCourseController {

    @Autowired
    private IStudentCourseService studentCourseService;

    @PutMapping("/update")
    public void update(@RequestBody StudentCourse course) {
        studentCourseService.update(course);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
        studentCourseService.delete(id);
    }

    @PostMapping("/add")
    public void add(@RequestBody StudentCourse course) {
        studentCourseService.add(course);
    }
    @GetMapping("/list")
    public IPage<StudentCourse> list(
            @RequestParam(required = false) String search,  // 改为课程号
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        Page<StudentCourse> page = new Page<>(current, size);
        QueryWrapper<StudentCourse> wrapper = new QueryWrapper<>();

        // 关键修改：同时搜索 studentNo 和 courseNo
        if (search != null && !search.trim().isEmpty()) {
            wrapper.lambda()
                    .eq(StudentCourse::getStudentNo, search)  // 匹配学生号
                    .or()
                    .eq(StudentCourse::getCourseNo, search);  // 匹配课程号
        }
        return studentCourseService.queryStudentCoursePage(page, wrapper);
    }

}

